//
//  SkypeStatz.m
//  Statz
//
//  Created by dmaclach on 6/17/07.
//
// Copyright 2007 Google Inc.
// 
// Licensed under the Apache License, Version 2.0 (the "License"); you may not
// use this file except in compliance with the License.  You may obtain a copy
// of the License at
// 
// http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
// License for the specific language governing permissions and limitations under
//

#import "SkypeStatz.h"
#import "StatusUpdate.h"
#import "AEVTBuilder.h"

@implementation SkypeStatz

- (NSString*)appBundleID {
  return @"com.skype.skype";
}

- (NSString*)uuid {
  return @"296C3986-DE76-458A-BD32-86CBC0660699";
}

- (StatzUpdateValue)updateStatz:(StatusUpdate*)sender {
  NSString *newStatus = [sender status];
  NSString *newState;
  switch ([sender state]) {
    case kStatzStateAvailable:
      newState = @"ONLINE";
      break;
    case kStatzStateAway:
      newState = @"AWAY";
      break;
    case kStatzStateDoNotDisturb:
      newState = @"DND";
      break;
    case kStatzStateContactMe:
      newState = @"SKYPEME";
      break;
    case kStatzStateInvisible:
      newState = @"INVISIBLE";
      break;
      
    case kStatzStateOffline:
      newState = @"OFFLINE";
      break;
    
    default:
      NSLog(@"Unknown state %d", [sender state]);
      return kStatzUpdateError;
      break;
  }
      
  NSAppleEventDescriptor *descriptor = [AEVT class:'send' 
                                                id:'skyp'
                                    targetBundleID:[self appBundleID],
                                        [KEY : 'cmnd'],[STRING : @"GET PROFILE MOOD_TEXT"],
                                        [KEY : 'scrp'],[STRING : @"Statz"],
                                        ENDRECORD];
  require_ae_noerr(descriptor, cantUpdate);
  NSAppleEventDescriptor *reply = [descriptor sendWithImmediateReply];
  require_ae_noerr(reply, cantUpdate);
  NSAppleEventDescriptor *keyDirectDesc = [reply descriptorForKeyword:keyDirectObject];
  require_ae_noerr(keyDirectDesc, cantUpdate);
  
  NSString *oldStatus = [keyDirectDesc stringValue];
  unsigned int length = [@"PROFILE MOOD_TEXT " length];
  if (length <= [oldStatus length]) {
    oldStatus = [oldStatus substringFromIndex:length];
  }
  
  descriptor = [AEVT class:'send' 
                        id:'skyp'
            targetBundleID:[self appBundleID],
                [KEY : 'cmnd'],[STRING : @"GET USERSTATUS"],
                [KEY : 'scrp'],[STRING : @"Statz"],
                ENDRECORD];
  require_ae_noerr(descriptor, cantUpdate);
  reply = [descriptor sendWithImmediateReply];
  require_ae_noerr(reply, cantUpdate);
  keyDirectDesc = [reply descriptorForKeyword:keyDirectObject];
  require_ae_noerr(keyDirectDesc, cantUpdate);
  NSString *oldState = [keyDirectDesc stringValue];
  length = [@"USERSTATUS " length];
  if (length <= [oldState length]) {
    oldState = [oldState substringFromIndex:length];
  }
  if ([oldStatus isEqualToString:newStatus] 
      && [oldState isEqualToString:newState]) {
    return kStatzUpdateNotChanged;
  }
  
  NSString *moodString = [NSString stringWithFormat:@"SET PROFILE MOOD_TEXT %@", newStatus];
  
  descriptor = [AEVT class:'send' 
                        id:'skyp'
            targetBundleID:[self appBundleID],
                [KEY : 'cmnd'],[STRING : moodString],
                [KEY : 'scrp'],[STRING : @"Statz"],
               ENDRECORD];
  require_ae_noerr(descriptor, cantUpdate);
  reply = [descriptor sendWithImmediateReply];
  require_ae_noerr(reply, cantUpdate);
  
  NSString *statusString = [NSString stringWithFormat:@"SET USERSTATUS %@", newState];
  descriptor = [AEVT class:'send' 
                        id:'skyp'
            targetBundleID:[self appBundleID],
                [KEY : 'cmnd'],[STRING : statusString],
                [KEY : 'scrp'],[STRING : @"Statz"],
                ENDRECORD];
  require_ae_noerr(descriptor, cantUpdate);
  reply = [descriptor sendWithImmediateReply];
  require_ae_noerr(reply, cantUpdate);
  
  return kStatzUpdateSuccess;
cantUpdate:
  return kStatzUpdateError;
}

- (unsigned int)stateMenuMask {
  return kStatzStateAvailable 
  | kStatzStateAway 
  | kStatzStateDoNotDisturb 
  | kStatzStateInvisible
  | kStatzStateOffline;
}
@end
